//
//  RegistSuccess.m
//  YaoHeAPP
//
//  Created by mac on 15/11/30.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "RegistSuccess.h"
#import "Masonry.h"
#import "CreateCompanyVC.h"
#import "Controller.h"
#import "Tool.h"
@interface RegistSuccess ()
{
    UIImageView*successImage;
    UIButton*successBtn;
    CALayer*_newLayer;
}
@end

@implementation RegistSuccess

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"RegistSuccess"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"RegistSuccess"];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    //调一个背景色
    UIColor*backColor=[UIColor whiteColor];
    UIColor*alphColor=[backColor colorWithAlphaComponent:0.96];
    self.view.backgroundColor=alphColor;

    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(60);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"注册成功";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker*make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];
    
    //右侧加入公司的按钮
    UIButton*rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"加入公司" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(jion) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-6);
        make.width.offset(80);
        make.height.offset(28);
        make.bottom.offset(-6);
    }];
    
    //注册成功的图片
    UIImageView*successImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"seccessful"]];
    [self.view addSubview:successImg];
    [successImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navcBar.mas_bottom).offset(45);
        make.left.offset(width/2-33);
        make.height.offset(66*1.1);
        make.right.offset(-(width/2-33));
    }];
    
    UIView*backView=[[UIView alloc]init];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.equalTo(successImg.mas_bottom).offset(15);
        make.height.offset(25);
    }];
    //恭喜这两个字
    UILabel*label1=[[UILabel alloc]init];
//    label1.backgroundColor=[UIColor yellowColor];
    label1.text=@"恭喜";
    label1.textColor=[UIColor blackColor];
    [backView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.bottom.offset(0);
        make.left.offset(width/2-118);
        make.width.offset(40);
    }];
    //显示电话号码的label
    UILabel*numberLab=[[UILabel alloc]init];
//    numberLab.backgroundColor=[UIColor yellowColor];
    numberLab.text=[NSString stringWithFormat:@"%@",self.numberPhone];
    numberLab.textColor=[UIColor greenColor];
    [backView addSubview:numberLab];
    [numberLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label1.mas_right).offset(2);
        make.height.offset(25);
        make.width.offset(115);
        make.top.offset(0);
    }];
    //注册成功这几个字的label
    UILabel*label2=[[UILabel alloc]init];
//    label2.backgroundColor=[UIColor yellowColor];
    label2.textColor=[UIColor blackColor];
    label2.text=@"注册成功!";
    [backView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(numberLab.mas_right).offset(2);
        make.top.offset(0);
        make.height.offset(25);
        make.width.offset(80);
    }];
    
    //剩下的文字
    UILabel*textLabel=[[UILabel alloc]init];
    textLabel.textColor=[UIColor grayColor];
    textLabel.numberOfLines=0;
    textLabel.text=@"欢迎加入吆喝!\n你还没有创建或加入任何公司!";
    textLabel.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.equalTo(backView.mas_bottom).offset(2);
        make.height.offset(50);
    }];
    
    
    //创建公司
    UIButton*loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"创建公司" forState:UIControlStateNormal];
    UIImage*loginOldImg=[UIImage imageNamed:@"loginbutton"];
    UIImage*loginNewImg=[loginOldImg stretchableImageWithLeftCapWidth:15 topCapHeight:0];
    [loginBtn setBackgroundImage:loginNewImg forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(createCompany) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(10);
        make.left.offset(33);
        make.right.offset(-33);
        make.height.offset(40);
    }];
    
    //如何加入公司按钮
    UIButton*registBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [registBtn setTitleColor:[UIColor colorWithRed:0.f/255.f green:179.f/255.f blue:249.f/255.f alpha:1] forState:UIControlStateNormal];
    [registBtn setTitle:@"加入公司" forState:UIControlStateNormal];
    UIImage*registOldImg=[UIImage imageNamed:@"ifaddbut_38"];
    UIImage*registNewImg=[registOldImg stretchableImageWithLeftCapWidth:19 topCapHeight:0];
    [registBtn setBackgroundImage:registNewImg forState:UIControlStateNormal];
    [registBtn addTarget:self action:@selector(jion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registBtn];
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(10);
        make.left.offset(33);
        make.right.offset(-33);
        make.height.offset(40);
    }];

}

-(void)imageAllow
{
    
    _newLayer = [CALayer layer];
    //相对于自身的大小
    //_newLayer.bounds = CGRectMake(0, 0, width-60, (width-60)*1.4);
    //position位置    设置_newLayer的中心点坐标
    _newLayer.position = CGPointMake(width/2, height/2);
    //_newLayer.backgroundColor = [UIColor whiteColor].CGColor;
    //_newLayer要展示的内容（需要转换类型）
    _newLayer.contents = (id)[UIImage imageNamed:@"ic_zhijian_libao"].CGImage;
    //在view的图层上添加一个核心动画层
    [self.view.layer addSublayer:_newLayer];
    CATransition *animation = [CATransition animation];
    animation.type = @"kCATransitionFade";
    //动画持续时间
    animation.duration = 2.5;
    //将动画添加到核心动画
    [_newLayer addAnimation:animation forKey:nil];
    _newLayer.bounds = CGRectMake(0, 0, width-60, (width-60)*1.4);

    
    /*
    successImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_zhijian_libao"]];
    [self.view addSubview:successImage];
    [self.view bringSubviewToFront:successImage];
    [successImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(30);
        make.right.offset(-30);
        make.top.offset((height-(width-60)*1.4)/2);
        make.height.offset((width-60)*1.4);
    }];
*/
    successBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    successBtn.frame=CGRectMake(15+_newLayer.frame.size.width, (height-_newLayer.frame.size.height)/2+15, 30, 30);
    [successBtn setBackgroundImage:[UIImage imageNamed:@"cancelimage"] forState:UIControlStateNormal];
    [successBtn addTarget:self action:@selector(clickRemove) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:successBtn];
    
}

//点击之后移除图片
-(void)clickRemove
{
    _newLayer.bounds=CGRectMake(0, 0, 0, 0);
    [successBtn removeFromSuperview];
}
//点击创建公司进入创建公司页面
-(void)createCompany
{
    [MobClick event:@"createACompany"];
    
    CreateCompanyVC*creatVC=[CreateCompanyVC new];
    [self.navigationController pushViewController:creatVC animated:YES];
}

//加入公司的按钮
-(void)jion
{
    [MobClick event:@"joinTheCompany"];
    
    UIAlertView*alertViewCT=[[UIAlertView alloc]initWithTitle:@"加入公司" message:@"请输入公司给你的邀请码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即加入", nil];
    alertViewCT.alertViewStyle=UIAlertViewStylePlainTextInput;
    alertViewCT.tag=10;
    [alertViewCT show];
}

//警告框的协议方法 下方两个按钮的索引
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10) {
        if (buttonIndex==1)
        {
            //获取输入的邀请码
            UITextField*textField=[alertView textFieldAtIndex:0];
            NSDictionary*dic=@{@"COMPANY_ID":textField.text};
            [Controller requestWithURL:@"AppCompanyRegist/PostBindCompany?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
                if ([classData.number integerValue]==0) {
                    UIAlertView*alertViewCT=[[UIAlertView alloc]initWithTitle:@"加入公司" message:@"加入公司成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    //alertViewCT.alertViewStyle=UIAlertViewStylePlainTextInput;
                    alertViewCT.tag=20;
                    [alertViewCT show];
                    
                    NSString *IDnumString=@"123456";
                    
                    [[NSUserDefaults standardUserDefaults] setObject:IDnumString forKey:ISCREATE];
                    
                    
                    NSString *isregtype=@"0";
                    [[NSUserDefaults standardUserDefaults] setObject:isregtype forKey:ISREGTYPE];

                    
                }
                else{
                    ALERTSHOW(classData.message);
                }
            }];
        }
    }
    
    if (alertView.tag==20) {
        RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
        [rootTabBarViewController changeRootViewControllerTo:0];
    }
}


//返回登录界面
- (void)backLogin {
    //进入主页
    RootTabBarViewController *rootTabBarViewController = [RootTabBarViewController rootViewController];
    [rootTabBarViewController changeRootViewControllerTo:0];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}










/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
